package com.spzx.payment.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.domain.ExtUserInfo;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.common.rabbit.service.RabbitService;
import com.spzx.payment.config.AlipayConfig;
import com.spzx.payment.domain.PaymentInfo;
import com.spzx.payment.service.AlipayService;
import com.spzx.payment.service.PaymentInfoService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class AlipayServiceImpl implements AlipayService {


    @Autowired
    private PaymentInfoService paymentInfoService;

    @Autowired
    private AlipayClient alipayClient;

    @Autowired
    private RabbitService rabbitService;

    @SneakyThrows
    @Override
    public String submitAlipay(String orderNo) {
        //调用PaymentInfoService中根据订单号查询支付记录的方法
        PaymentInfo paymentInfo= paymentInfoService.getPaymentInfoByOrder(orderNo);


        // 构造请求参数以调用接口
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();

        // 设置商户订单号
        model.setOutTradeNo(orderNo);

        // 设置订单总金额
        model.setTotalAmount(paymentInfo.getAmount().toString());
        //model.setTotalAmount("0.01");

        // 设置订单标题
        model.setSubject(paymentInfo.getContent());

        // 设置产品码
        model.setProductCode("QUICK_WAP_WAY");





        request.setBizModel(model);


        //设置支付成功跳转页面
        request.setReturnUrl(AlipayConfig.return_payment_url);
        //设置支付成功之后支付宝回调商户后台接口的地址
        request.setNotifyUrl(AlipayConfig.notify_payment_url);



        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        // AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println("=============================================");
        System.out.println(pageRedirectionData);

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }

        return "";
    }

    /**
     * 更新支付状态
     * @param paramMap
     */
    @Override
    public void updatePaymentInfoStatus(Map<String, String> paramMap) {
        //获取订单号
        String orderNo = paramMap.get("out_trade_no");
        //根据订单号查询支付记录
        PaymentInfo paymentInfo = paymentInfoService.getPaymentInfoByOrder(orderNo);
        //判断
        if(paymentInfo != null && paymentInfo.getPaymentStatus() == 1){
            //已经更新过
            return;
        }
        //获取交易号
        String tradeNo = paramMap.get("trade_no");
        //设置交易号
        paymentInfo.setTradeNo(tradeNo);
        //设置回调时间
        paymentInfo.setCallbackTime(new Date());
        //设置回调内容
        paymentInfo.setCallbackContent(paramMap.toString());
        //设置支付状态
        paymentInfo.setPaymentStatus(1);
        //设置更新时间
        paymentInfo.setUpdateTime(new Date());
        //调用根据id更新的方法
        paymentInfoService.updateById(paymentInfo);

        //TODO：1.向RabbitMQ发送消息更新订单状态
        rabbitService.sendMessage(MqConst.EXCHANGE_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,orderNo);
        //TODO：2.向RabbitMQ发送消息减库存加销量
        rabbitService.sendMessage(MqConst.EXCHANGE_PRODUCT,MqConst.ROUTING_MINUS,orderNo);

    }
}
